# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=artifactory-oss
_pkgname=artifactory
pkgver=4.16.1
pkgrel=0
pkgdesc="Open source Maven repository manager"
url="https://www.jfrog.com/open-source/"
arch="noarch !mips !mips64" # java
license="AGPL-3.0"
depends="jetty-runner openjdk8-jre-base"
makedepends="unzip zip"
install="$pkgname.pre-install $pkgname.post-install"
pkgusers="artifact"
pkggroups="artifact"
source="https://jcenter.bintray.com/org/artifactory/oss/jfrog-$pkgname/$pkgver/jfrog-$pkgname-$pkgver.zip
	$_pkgname.confd"
builddir="$srcdir/$pkgname-$pkgver"

prepare() {
	default_prepare || return 1

	cd "$builddir"

	# Prepare default storage config (this is copied from Artifactory).
	cat > etc/storage.properties <<-'EOF'
		type=derby
		url=jdbc:derby:{db.home};create=true
		driver=org.apache.derby.jdbc.EmbeddedDriver
	EOF

	# Allow to change logs directory using system property.
	sed -i 's|${artifactory.home}/logs|${artifactory.logs}|' \
		etc/logback.xml || return 1

	cd webapps

	# Remove tomcat-juli from WAR, it conflicts with Jetty's implementation.
	zip -d $_pkgname.war WEB-INF/lib/tomcat-juli-*.jar || return 1

	# Patch web.xml inside WAR; remove second declaration of
	# welcome-file-list that overwrites the previous correct one.
	unzip $_pkgname.war WEB-INF/web.xml || return 1
	patch -l WEB-INF/web.xml <<-EOF || return 1
		@@ -159,8 +159,5 @@
		         <url-pattern>/ui/*</url-pattern>
		     </servlet-mapping>

		-    <welcome-file-list>
		-        <welcome-file>TC7-49422</welcome-file>
		-    </welcome-file-list>

		 </web-app>
	EOF
	zip -u $_pkgname.war WEB-INF/web.xml
}

package() {
	local libdir="$pkgdir/var/lib/$_pkgname"

	cd "$builddir"

	install -m 644 -D webapps/$_pkgname.war \
		"$pkgdir"/usr/share/java/$_pkgname.war || return 1

	install -m 750 -o $pkgusers -g $pkggroups -d \
		"$libdir" "$libdir"/etc "$libdir"/data || return 1

	install -m 755 -o $pkgusers -g $pkggroups -d \
		"$pkgdir"/var/log/$_pkgname || return 1

	# Derby insists on logging into ./logs, so we must symlink it.
	ln -s /var/log/$_pkgname "$libdir"/logs || return 1

	local f
	for f in artifactory.system.properties mimetypes.xml storage.properties; do
		install -m 644 -D etc/$f "$pkgdir"/etc/$_pkgname/$f || return 1
		ln -s /etc/$_pkgname/$f "$libdir"/etc/$f || return 1
	done

	# Fix permissions, this file may contain DB password.
	install -m 640 -o root -g $pkggroups etc/storage.properties \
		"$pkgdir"/etc/$_pkgname/storage.properties || return 1

	# Artifactory dumps settings from web admin interface, so keep it in
	# /usr/lib instead of /etc.
	install -m 600 -o $pkgusers -g $pkggroups \
		etc/artifactory.config.xml "$libdir"/etc/ || return 1

	# Artifactory doesn't like symlinked logback.xml, so we must keep it here.
	install -m 644 -o $pkgusers -g $pkggroups \
		etc/logback.xml "$libdir"/etc/ || return 1

	mkdir -p "$pkgdir"/etc/init.d
	ln -s jetty-runner "$pkgdir"/etc/init.d/$_pkgname || return 1

	install -m 644 -D "$srcdir"/$_pkgname.confd "$pkgdir"/etc/conf.d/$_pkgname
}

sha512sums="21999e0f8741f68c8a075132238f823532c1216d2828e1ce3e79dbb5a11687cc6bda60d9493002e4eada177a761e9fcacf08b99e3bcd2af75e1c76574a2eb6fb  jfrog-artifactory-oss-4.16.1.zip
2701ea102da77b86be3b9aa75d5aa92fd5d81081ae1c835aaea49b0f0fd015bd5b32128ba913afda10fac122eec2d0bd1e203af4ee3d13106c3e66e7d1b83498  artifactory.confd"
